<?php

require_once 'class.character.php';

class message extends character
{
	public $property = array();
	
	public function __construct()
	{
		$this -> open();
	}
	
	public function getMsg()
	{
		return $this -> property;
	}

	public function setMsg($query)
	{
		$this -> query($query);
	
			while($rd = $this -> fetch())
			{
				$this -> property[] = $rd;
			}
		
		return $this;
	}

    public function msgRead($id)
    {
        $this -> query('UPDATE mail SET mail_read = 1 WHERE mail_id = ' . mysql_real_escape_string($id));
    }

    public function msgDelete($id_or_array, $type = 'mail')
    {
    	
		if(is_integer($id_or_array))
		{
			$in_id[0] = msgView($id_or_array);
			
		} else if(is_array($id_or_array)) 
		{
			$in_id = msgView($id_or_array, TRUE);
		} else {
			return FALSE;
		}
		
		for($i = 0; $i < count($data); $i++)
		{	
			if($data[$i][$type . '_archive'] == 0)
			{
        		$this -> query($sql = 'DELETE FROM ' . $type . ' WHERE ' . $type . '_id = ' . mysql_real_escape_string($data[$i][$type . '_id']));
			} else {
				$this -> query($sql = 'UPDATE ' . $type . ' SET ' . $type . '_cancel = 1 WHERE ' . $type . '_id = ' . mysql_real_escape_string($data[$i][$type . '_id']));
			}
		}

    }
    
    public function msgInbox()
    {
    	$this -> open();
    	$this -> query("SELECT mail_id, mail_date, mail_from, mail_object, mail_text FROM mail WHERE mail_to = " . $_SESSION['cid']);
    }
    
    public function msgSend($array = array(), $type = '')
    {
    	switch($type)
    	{
    		case 'global':
    			
    		break;
    		case 'guild':
    			
    		break;
    		case 'race':
    			
    		break;
    		default:
    			$id = $this -> convName($array['id']);
    			 
    			$this -> setUser($id);
    			$char = $this -> getUser();
    			if($char['character_id'] == $array['id'])
    			{
    				$this -> query('INSERT INTO VALUES (NULL, ' . $array['mail_from'] . ', ' . $array['mail_to'] . ', NOW(), 0, ' . $array['mail_text'] . ', NULL, 0, ' . $array['mail_object'] . ')');
    			}
    		break;
    	}
    }
	
	function __destruct()
	{
		if($this -> db != NULL)
			$this -> close();
	}
		
}

?>